<template>
  <div id="details">
    <!-- 头部 -->
    <div class="page-header">
      <div class="title">
        <p>{{ productDetails.product_name }}</p>
        <div class="list">
          <ul>
            <li>
              <router-link to>概述</router-link>
            </li>
            <li>
              <router-link to>参数</router-link>
            </li>
            <li>
              <router-link to>用户评价</router-link>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <!-- 头部END -->

    <!-- 主要内容 -->
    <div class="main">
      <!-- 左侧商品轮播图 -->
      <div class="block">
        <el-carousel height="560px" v-if="productPicture.length > 1">
          <el-carousel-item v-for="item in productPicture" :key="item.id">
            <img style="height: 560px;" :src="$target + item.product_picture" :alt="item.intro" />
          </el-carousel-item>
        </el-carousel>
        <div v-if="productPicture.length == 1">
          <img
            style="height: 560px;"
            :src="$target + productPicture[0].product_picture"
            :alt="productPicture[0].intro"
          />
        </div>
      </div>
      <!-- 左侧商品轮播图END -->

      <!-- 右侧内容区 -->
      <div class="content">
        <h1 class="name">{{ productDetails.product_name }}</h1>
        <p class="intro">{{ productDetails.product_intro }}</p>
        <p class="store">小米自营</p>
        <div class="price">
          <span>{{ productDetails.product_selling_price }}元</span>
          <span v-show="productDetails.product_price != productDetails.product_selling_price" class="del"
            >{{ productDetails.product_price }}元</span
          >
        </div>
        <div class="pro-list">
          <span class="pro-name">{{ productDetails.product_name }}</span>
          <span class="pro-price">
            <span>{{ productDetails.product_selling_price }}元</span>
            <span v-show="productDetails.product_price != productDetails.product_selling_price" class="pro-del"
              >{{ productDetails.product_price }}元</span
            >
          </span>
          <p class="price-sum">总计 : {{ productDetails.product_selling_price }}元</p>
        </div>
        <!-- 内容区底部按钮 -->
        <div class="button">
          <el-button class="shop-cart" :disabled="dis" @click="addShoppingCart">加入购物车</el-button>
          <el-button class="like" @click="addCollect">喜欢</el-button>
        </div>
        <!-- 内容区底部按钮END -->
        <div class="pro-policy">
          <ul>
            <li><i class="el-icon-circle-check"></i> 小米自营</li>
            <li><i class="el-icon-circle-check"></i> 小米发货</li>
            <li><i class="el-icon-circle-check"></i> 7天无理由退货</li>
            <li><i class="el-icon-circle-check"></i> 7天价格保护</li>
          </ul>
        </div>
      </div>
      <!-- 右侧内容区END -->
    </div>
    <!-- 主要内容END -->
  </div>
</template>
<script>
import { mapActions } from 'vuex'
export default {
  data() {
    return {
      dis: false, // 控制“加入购物车按钮是否可用”
      productID: '', // 商品id
      productDetails: '', // 商品详细信息
      productPicture: '' // 商品图片
    }
  },
  // 通过路由获取商品id
  activated() {
    if (this.$route.query.productID !== undefined) {
      this.productID = this.$route.query.productID
    }
  },
  watch: {
    // 监听商品id的变化，请求后端获取商品数据
    productID: function (val) {
      this.getDetails(val)
      this.getDetailsPicture(val)
    }
  },
  methods: {
    ...mapActions(['unshiftShoppingCart', 'addShoppingCartNum']),
    // 获取商品详细信息
    getDetails(val) {
      this.$axios
        .post('/product/getDetails', {
          productID: val
        })
        .then((res) => {
          this.productDetails = res.data.Product[0]
        })
        .catch((err) => {
          return Promise.reject(err)
        })
    },
    // 获取商品图片
    getDetailsPicture(val) {
      this.$axios
        .post('/product/getDetailsPicture', {
          productID: val
        })
        .then((res) => {
          this.productPicture = res.data.ProductPicture
        })
        .catch((err) => {
          return Promise.reject(err)
        })
    },
    // 加入购物车
    addShoppingCart() {
      // 判断是否登录,没有登录则显示登录组件
      if (!this.$store.getters.getUser) {
        this.$store.dispatch('setShowLogin', true)
        return
      }
      this.$axios
        .post('/user/shoppingCart/addShoppingCart', {
          user_id: this.$store.getters.getUser.user_id,
          product_id: this.productID
        })
        .then((res) => {
          switch (res.data.code) {
            case '001':
              // 新加入购物车成功
              this.unshiftShoppingCart(res.data.shoppingCartData[0])
              this.notifySucceed(res.data.msg)
              break
            case '002':
              // 该商品已经在购物车，数量+1
              this.addShoppingCartNum(this.productID)
              this.notifySucceed(res.data.msg)
              break
            case '003':
              // 商品数量达到限购数量
              this.dis = true
              this.notifyError(res.data.msg)
              break
            default:
              this.notifyError(res.data.msg)
          }
        })
        .catch((err) => {
          return Promise.reject(err)
        })
    },
    addCollect() {
      // 判断是否登录,没有登录则显示登录组件
      if (!this.$store.getters.getUser) {
        this.$store.dispatch('setShowLogin', true)
        return
      }
      this.$axios
        .post('/user/collect/addCollect', {
          user_id: this.$store.getters.getUser.user_id,
          product_id: this.productID
        })
        .then((res) => {
          if (res.data.code === '001') {
            // 添加收藏成功
            this.notifySucceed(res.data.msg)
          } else {
            // 添加收藏失败
            this.notifyError(res.data.msg)
          }
        })
        .catch((err) => {
          return Promise.reject(err)
        })
    }
  }
}
</script>
